package datastructurs.sort;

import java.util.Arrays;

//希尔排序
public class ShellSort {
    public static void main(String[] args) {
        int[] arr = { 8, 9, 1, 7, 2, 3, 5, 4, 6, 0,13 };
        System.out.println("排序前：");
        System.out.println(Arrays.toString(arr));
        shellSort(arr);
        System.out.println("排序后：");
        System.out.println(Arrays.toString(arr));
    }
    public static void shellSort(int[] arr){
        for(int grap=arr.length/2;grap>0;grap/=2){
            for(int i=grap;i<arr.length;i++){
                int j = i;
                int temp = arr[j];
                if(arr[j]<arr[j-grap]){
                    while(j-grap>=0&&temp<arr[j-grap]){
                        arr[j] = arr[j-grap];
                        j -= grap;
                    }
                    arr[j] = temp;
                }
            }
        }
    }
}
